package com.sword.service.impl.buy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sword.constant.MessageConstant;
import com.sword.page.PageResult;
import com.sword.page.QueryPageBean;
import com.sword.pojo.buy.BuyorderDetail;
import com.sword.service.buy.BuyorderDetailService;
import com.sword.service.impl.BasicBaseServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
@Service
public class BuyorderDetailImpl extends BasicBaseServiceImpl<BuyorderDetail> implements BuyorderDetailService {
    @Override
    public PageResult queryBuyorderDetail(QueryPageBean queryPageBean) {
        QueryWrapper<BuyorderDetail> wrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        // 如果queryString有值
        if (queryString != null) {
            BuyorderDetail buyorderDetail = new BuyorderDetail();
            Field[] fields = buyorderDetail.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                //有的字段是用private修饰的 将他设置为可读
                fields[i].setAccessible(true);
                if(fields[i].getName().endsWith("Date")){
                    continue;
                }
                wrapper.like(fields[i].getName(),queryString).or();
            }
        }
        IPage<BuyorderDetail> iPage = super.queryPageList(wrapper, queryPageBean.getPage(), queryPageBean.getLimit());
        return new PageResult(0, MessageConstant.QUERY_BUYORDERDETAIL_SUCCESS, iPage.getTotal(), iPage.getRecords());
    }


    @Override
    public Integer addBuyorderDetail(BuyorderDetail buyorderDetail) {
        return super.save(buyorderDetail);
    }

    @Override
    public Integer editBuyorderDetail(BuyorderDetail buyorderDetail) {
        return super.update(buyorderDetail);
    }

    @Override
    public Integer examineBuyorderDetail(BuyorderDetail buyorderDetail) {
        String userId = (String) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        buyorderDetail.setExamineid(userId);
        buyorderDetail.setStatus(1);
        return super.update(buyorderDetail);
    }

    @Override
    public Integer unexamineBuyorderDetail(BuyorderDetail buyorderDetail) {
        buyorderDetail.setExamineid("");
        buyorderDetail.setStatus(0);
        return super.update(buyorderDetail);
    }

    @Override
    public Integer deleteBuyorderDetail(Integer id) {
        return super.deleteById(Long.valueOf(id));
    }
}
